Import pre-release gcc-5.0 to new vendor branch
[dragonfly.git] / contrib / gcc-5.0 / libstdc++-v3 / include / bits / stl_deque.h
1 // Deque implementation -*- C++ -*-
2
3 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation.  Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose.  It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1997
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation.  Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose.  It is provided "as is" without express or implied warranty.
49  */
50
51 /** @file bits/stl_deque.h
52  *  This is an internal header file, included by other library headers.
53  *  Do not attempt to use it directly. @headername{deque}
54  */
55
56 #ifndef _STL_DEQUE_H
57 #define _STL_DEQUE_H 1
58
59 #include <bits/concept_check.h>
60 #include <bits/stl_iterator_base_types.h>
61 #include <bits/stl_iterator_base_funcs.h>
62 #if __cplusplus >= 201103L
63 #include <initializer_list>
64 #endif
65
66 namespace std _GLIBCXX_VISIBILITY(default)
67 {
68 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
69
70   /**
71    *  @brief This function controls the size of memory nodes.
72    *  @param  __size  The size of an element.
73    *  @return   The number (not byte size) of elements per node.
74    *
75    *  This function started off as a compiler kludge from SGI, but
76    *  seems to be a useful wrapper around a repeated constant
77    *  expression.  The @b 512 is tunable (and no other code needs to
78    *  change), but no investigation has been done since inheriting the
79    *  SGI code.  Touch _GLIBCXX_DEQUE_BUF_SIZE only if you know what
80    *  you are doing, however: changing it breaks the binary
81    *  compatibility!!
82   */
83
84 #ifndef _GLIBCXX_DEQUE_BUF_SIZE
85 #define _GLIBCXX_DEQUE_BUF_SIZE 512
86 #endif
87
88   _GLIBCXX_CONSTEXPR inline size_t
89   __deque_buf_size(size_t __size)
90   { return (__size < _GLIBCXX_DEQUE_BUF_SIZE
91             ? size_t(_GLIBCXX_DEQUE_BUF_SIZE / __size) : size_t(1)); }
92
93
94   /**
95    *  @brief A deque::iterator.
96    *
97    *  Quite a bit of intelligence here.  Much of the functionality of
98    *  deque is actually passed off to this class.  A deque holds two
99    *  of these internally, marking its valid range.  Access to
100    *  elements is done as offsets of either of those two, relying on
101    *  operator overloading in this class.
102    *
103    *  All the functions are op overloads except for _M_set_node.
104   */
105   template<typename _Tp, typename _Ref, typename _Ptr>
106     struct _Deque_iterator
107     {
108 #if __cplusplus < 201103L
109       typedef _Deque_iterator<_Tp, _Tp&, _Tp*>             iterator;
110       typedef _Deque_iterator<_Tp, const _Tp&, const _Tp*> const_iterator;
111       typedef _Tp*                                         _Elt_pointer;
112       typedef _Tp**                                        _Map_pointer;
113 #else
114     private:
115       template<typename _Up>
116         using __ptr_to = typename pointer_traits<_Ptr>::template rebind<_Up>;
117       template<typename _CvTp>
118         using __iter = _Deque_iterator<_Tp, _CvTp&, __ptr_to<_CvTp>>;
119     public:
120       typedef __iter<_Tp>               iterator;
121       typedef __iter<const _Tp>         const_iterator;
122       typedef __ptr_to<_Tp>             _Elt_pointer;
123       typedef __ptr_to<_Elt_pointer>    _Map_pointer;
124 #endif
125
126       static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
127       { return __deque_buf_size(sizeof(_Tp)); }
128
129       typedef std::random_access_iterator_tag iterator_category;
130       typedef _Tp                             value_type;
131       typedef _Ptr                            pointer;
132       typedef _Ref                            reference;
133       typedef size_t                          size_type;
134       typedef ptrdiff_t                       difference_type;
135       typedef _Deque_iterator                 _Self;
136
137       _Elt_pointer _M_cur;
138       _Elt_pointer _M_first;
139       _Elt_pointer _M_last;
140       _Map_pointer _M_node;
141
142       _Deque_iterator(_Elt_pointer __x, _Map_pointer __y) _GLIBCXX_NOEXCEPT
143       : _M_cur(__x), _M_first(*__y),
144         _M_last(*__y + _S_buffer_size()), _M_node(__y) { }
145
146       _Deque_iterator() _GLIBCXX_NOEXCEPT
147       : _M_cur(), _M_first(), _M_last(), _M_node() { }
148
149       _Deque_iterator(const iterator& __x) _GLIBCXX_NOEXCEPT
150       : _M_cur(__x._M_cur), _M_first(__x._M_first),
151         _M_last(__x._M_last), _M_node(__x._M_node) { }
152
153       iterator
154       _M_const_cast() const _GLIBCXX_NOEXCEPT
155       { return iterator(_M_cur, _M_node); }
156
157       reference
158       operator*() const _GLIBCXX_NOEXCEPT
159       { return *_M_cur; }
160
161       pointer
162       operator->() const _GLIBCXX_NOEXCEPT
163       { return _M_cur; }
164
165       _Self&
166       operator++() _GLIBCXX_NOEXCEPT
167       {
168         ++_M_cur;
169         if (_M_cur == _M_last)
170           {
171             _M_set_node(_M_node + 1);
172             _M_cur = _M_first;
173           }
174         return *this;
175       }
176
177       _Self
178       operator++(int) _GLIBCXX_NOEXCEPT
179       {
180         _Self __tmp = *this;
181         ++*this;
182         return __tmp;
183       }
184
185       _Self&
186       operator--() _GLIBCXX_NOEXCEPT
187       {
188         if (_M_cur == _M_first)
189           {
190             _M_set_node(_M_node - 1);
191             _M_cur = _M_last;
192           }
193         --_M_cur;
194         return *this;
195       }
196
197       _Self
198       operator--(int) _GLIBCXX_NOEXCEPT
199       {
200         _Self __tmp = *this;
201         --*this;
202         return __tmp;
203       }
204
205       _Self&
206       operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
207       {
208         const difference_type __offset = __n + (_M_cur - _M_first);
209         if (__offset >= 0 && __offset < difference_type(_S_buffer_size()))
210           _M_cur += __n;
211         else
212           {
213             const difference_type __node_offset =
214               __offset > 0 ? __offset / difference_type(_S_buffer_size())
215                            : -difference_type((-__offset - 1)
216                                               / _S_buffer_size()) - 1;
217             _M_set_node(_M_node + __node_offset);
218             _M_cur = _M_first + (__offset - __node_offset
219                                  * difference_type(_S_buffer_size()));
220           }
221         return *this;
222       }
223
224       _Self
225       operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
226       {
227         _Self __tmp = *this;
228         return __tmp += __n;
229       }
230
231       _Self&
232       operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
233       { return *this += -__n; }
234
235       _Self
236       operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
237       {
238         _Self __tmp = *this;
239         return __tmp -= __n;
240       }
241
242       reference
243       operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
244       { return *(*this + __n); }
245
246       /** 
247        *  Prepares to traverse new_node.  Sets everything except
248        *  _M_cur, which should therefore be set by the caller
249        *  immediately afterwards, based on _M_first and _M_last.
250        */
251       void
252       _M_set_node(_Map_pointer __new_node) _GLIBCXX_NOEXCEPT
253       {
254         _M_node = __new_node;
255         _M_first = *__new_node;
256         _M_last = _M_first + difference_type(_S_buffer_size());
257       }
258     };
259
260   // Note: we also provide overloads whose operands are of the same type in
261   // order to avoid ambiguous overload resolution when std::rel_ops operators
262   // are in scope (for additional details, see libstdc++/3628)
263   template<typename _Tp, typename _Ref, typename _Ptr>
264     inline bool
265     operator==(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
266                const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
267     { return __x._M_cur == __y._M_cur; }
268
269   template<typename _Tp, typename _RefL, typename _PtrL,
270            typename _RefR, typename _PtrR>
271     inline bool
272     operator==(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
273                const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
274     { return __x._M_cur == __y._M_cur; }
275
276   template<typename _Tp, typename _Ref, typename _Ptr>
277     inline bool
278     operator!=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
279                const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
280     { return !(__x == __y); }
281
282   template<typename _Tp, typename _RefL, typename _PtrL,
283            typename _RefR, typename _PtrR>
284     inline bool
285     operator!=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
286                const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
287     { return !(__x == __y); }
288
289   template<typename _Tp, typename _Ref, typename _Ptr>
290     inline bool
291     operator<(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
292               const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
293     { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
294                                           : (__x._M_node < __y._M_node); }
295
296   template<typename _Tp, typename _RefL, typename _PtrL,
297            typename _RefR, typename _PtrR>
298     inline bool
299     operator<(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
300               const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
301     { return (__x._M_node == __y._M_node) ? (__x._M_cur < __y._M_cur)
302                                           : (__x._M_node < __y._M_node); }
303
304   template<typename _Tp, typename _Ref, typename _Ptr>
305     inline bool
306     operator>(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
307               const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
308     { return __y < __x; }
309
310   template<typename _Tp, typename _RefL, typename _PtrL,
311            typename _RefR, typename _PtrR>
312     inline bool
313     operator>(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
314               const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
315     { return __y < __x; }
316
317   template<typename _Tp, typename _Ref, typename _Ptr>
318     inline bool
319     operator<=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
320                const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
321     { return !(__y < __x); }
322
323   template<typename _Tp, typename _RefL, typename _PtrL,
324            typename _RefR, typename _PtrR>
325     inline bool
326     operator<=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
327                const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
328     { return !(__y < __x); }
329
330   template<typename _Tp, typename _Ref, typename _Ptr>
331     inline bool
332     operator>=(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
333                const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
334     { return !(__x < __y); }
335
336   template<typename _Tp, typename _RefL, typename _PtrL,
337            typename _RefR, typename _PtrR>
338     inline bool
339     operator>=(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
340                const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
341     { return !(__x < __y); }
342
343   // _GLIBCXX_RESOLVE_LIB_DEFECTS
344   // According to the resolution of DR179 not only the various comparison
345   // operators but also operator- must accept mixed iterator/const_iterator
346   // parameters.
347   template<typename _Tp, typename _Ref, typename _Ptr>
348     inline typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
349     operator-(const _Deque_iterator<_Tp, _Ref, _Ptr>& __x,
350               const _Deque_iterator<_Tp, _Ref, _Ptr>& __y) _GLIBCXX_NOEXCEPT
351     {
352       return typename _Deque_iterator<_Tp, _Ref, _Ptr>::difference_type
353         (_Deque_iterator<_Tp, _Ref, _Ptr>::_S_buffer_size())
354         * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
355         + (__y._M_last - __y._M_cur);
356     }
357
358   template<typename _Tp, typename _RefL, typename _PtrL,
359            typename _RefR, typename _PtrR>
360     inline typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
361     operator-(const _Deque_iterator<_Tp, _RefL, _PtrL>& __x,
362               const _Deque_iterator<_Tp, _RefR, _PtrR>& __y) _GLIBCXX_NOEXCEPT
363     {
364       return typename _Deque_iterator<_Tp, _RefL, _PtrL>::difference_type
365         (_Deque_iterator<_Tp, _RefL, _PtrL>::_S_buffer_size())
366         * (__x._M_node - __y._M_node - 1) + (__x._M_cur - __x._M_first)
367         + (__y._M_last - __y._M_cur);
368     }
369
370   template<typename _Tp, typename _Ref, typename _Ptr>
371     inline _Deque_iterator<_Tp, _Ref, _Ptr>
372     operator+(ptrdiff_t __n, const _Deque_iterator<_Tp, _Ref, _Ptr>& __x)
373     _GLIBCXX_NOEXCEPT
374     { return __x + __n; }
375
376   template<typename _Tp>
377     void
378     fill(const _Deque_iterator<_Tp, _Tp&, _Tp*>&,
379          const _Deque_iterator<_Tp, _Tp&, _Tp*>&, const _Tp&);
380
381   template<typename _Tp>
382     _Deque_iterator<_Tp, _Tp&, _Tp*>
383     copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
384          _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
385          _Deque_iterator<_Tp, _Tp&, _Tp*>);
386
387   template<typename _Tp>
388     inline _Deque_iterator<_Tp, _Tp&, _Tp*>
389     copy(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
390          _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
391          _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
392     { return std::copy(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
393                        _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
394                        __result); }
395
396   template<typename _Tp>
397     _Deque_iterator<_Tp, _Tp&, _Tp*>
398     copy_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
399                   _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
400                   _Deque_iterator<_Tp, _Tp&, _Tp*>);
401
402   template<typename _Tp>
403     inline _Deque_iterator<_Tp, _Tp&, _Tp*>
404     copy_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
405                   _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
406                   _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
407     { return std::copy_backward(_Deque_iterator<_Tp,
408                                 const _Tp&, const _Tp*>(__first),
409                                 _Deque_iterator<_Tp,
410                                 const _Tp&, const _Tp*>(__last),
411                                 __result); }
412
413 #if __cplusplus >= 201103L
414   template<typename _Tp>
415     _Deque_iterator<_Tp, _Tp&, _Tp*>
416     move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
417          _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
418          _Deque_iterator<_Tp, _Tp&, _Tp*>);
419
420   template<typename _Tp>
421     inline _Deque_iterator<_Tp, _Tp&, _Tp*>
422     move(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
423          _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
424          _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
425     { return std::move(_Deque_iterator<_Tp, const _Tp&, const _Tp*>(__first),
426                        _Deque_iterator<_Tp, const _Tp&, const _Tp*>(__last),
427                        __result); }
428
429   template<typename _Tp>
430     _Deque_iterator<_Tp, _Tp&, _Tp*>
431     move_backward(_Deque_iterator<_Tp, const _Tp&, const _Tp*>,
432                   _Deque_iterator<_Tp, const _Tp&, const _Tp*>,
433                   _Deque_iterator<_Tp, _Tp&, _Tp*>);
434
435   template<typename _Tp>
436     inline _Deque_iterator<_Tp, _Tp&, _Tp*>
437     move_backward(_Deque_iterator<_Tp, _Tp&, _Tp*> __first,
438                   _Deque_iterator<_Tp, _Tp&, _Tp*> __last,
439                   _Deque_iterator<_Tp, _Tp&, _Tp*> __result)
440     { return std::move_backward(_Deque_iterator<_Tp,
441                                 const _Tp&, const _Tp*>(__first),
442                                 _Deque_iterator<_Tp,
443                                 const _Tp&, const _Tp*>(__last),
444                                 __result); }
445 #endif
446
447   /**
448    *  Deque base class.  This class provides the unified face for %deque's
449    *  allocation.  This class's constructor and destructor allocate and
450    *  deallocate (but do not initialize) storage.  This makes %exception
451    *  safety easier.
452    *
453    *  Nothing in this class ever constructs or destroys an actual Tp element.
454    *  (Deque handles that itself.)  Only/All memory management is performed
455    *  here.
456   */
457   template<typename _Tp, typename _Alloc>
458     class _Deque_base
459     {
460     protected:
461       typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
462         rebind<_Tp>::other _Tp_alloc_type;
463       typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type>  _Alloc_traits;
464
465 #if __cplusplus < 201103L
466       typedef _Tp*                                      _Ptr;
467       typedef const _Tp*                                _Ptr_const;
468 #else
469       typedef typename _Alloc_traits::pointer           _Ptr;
470       typedef typename _Alloc_traits::const_pointer     _Ptr_const;
471 #endif
472
473       typedef typename _Alloc_traits::template rebind<_Ptr>::other
474         _Map_alloc_type;
475       typedef __gnu_cxx::__alloc_traits<_Map_alloc_type> _Map_alloc_traits;
476
477     public:
478       typedef _Alloc                  allocator_type;
479       typedef typename _Alloc_traits::size_type size_type;
480
481       allocator_type
482       get_allocator() const _GLIBCXX_NOEXCEPT
483       { return allocator_type(_M_get_Tp_allocator()); }
484
485       typedef _Deque_iterator<_Tp, _Tp&, _Ptr>          iterator;
486       typedef _Deque_iterator<_Tp, const _Tp&, _Ptr_const>   const_iterator;
487
488       _Deque_base()
489       : _M_impl()
490       { _M_initialize_map(0); }
491
492       _Deque_base(size_t __num_elements)
493       : _M_impl()
494       { _M_initialize_map(__num_elements); }
495
496       _Deque_base(const allocator_type& __a, size_t __num_elements)
497       : _M_impl(__a)
498       { _M_initialize_map(__num_elements); }
499
500       _Deque_base(const allocator_type& __a)
501       : _M_impl(__a)
502       { /* Caller must initialize map. */ }
503
504 #if __cplusplus >= 201103L
505       _Deque_base(_Deque_base&& __x, false_type)
506       : _M_impl(__x._M_move_impl())
507       { }
508
509       _Deque_base(_Deque_base&& __x, true_type)
510       : _M_impl(std::move(__x._M_get_Tp_allocator()))
511       {
512         _M_initialize_map(0);
513         if (__x._M_impl._M_map)
514           this->_M_impl._M_swap_data(__x._M_impl);
515       }
516
517       _Deque_base(_Deque_base&& __x)
518       : _Deque_base(std::move(__x),
519                     __gnu_cxx::__allocator_always_compares_equal<_Alloc>{})
520       { }
521
522       _Deque_base(_Deque_base&& __x, const allocator_type& __a, size_type __n)
523       : _M_impl(__a)
524       {
525         if (__x.get_allocator() == __a)
526           {
527             if (__x._M_impl._M_map)
528               {
529                 _M_initialize_map(0);
530                 this->_M_impl._M_swap_data(__x._M_impl);
531               }
532           }
533         else
534           {
535             _M_initialize_map(__n);
536           }
537       }
538 #endif
539
540       ~_Deque_base() _GLIBCXX_NOEXCEPT;
541
542     protected:
543       typedef typename iterator::_Map_pointer _Map_pointer;
544
545       //This struct encapsulates the implementation of the std::deque
546       //standard container and at the same time makes use of the EBO
547       //for empty allocators.
548       struct _Deque_impl
549       : public _Tp_alloc_type
550       {
551         _Map_pointer _M_map;
552         size_t _M_map_size;
553         iterator _M_start;
554         iterator _M_finish;
555
556         _Deque_impl()
557         : _Tp_alloc_type(), _M_map(), _M_map_size(0),
558           _M_start(), _M_finish()
559         { }
560
561         _Deque_impl(const _Tp_alloc_type& __a) _GLIBCXX_NOEXCEPT
562         : _Tp_alloc_type(__a), _M_map(), _M_map_size(0),
563           _M_start(), _M_finish()
564         { }
565
566 #if __cplusplus >= 201103L
567         _Deque_impl(_Deque_impl&&) = default;
568
569         _Deque_impl(_Tp_alloc_type&& __a) noexcept
570         : _Tp_alloc_type(std::move(__a)), _M_map(), _M_map_size(0),
571           _M_start(), _M_finish()
572         { }
573 #endif
574
575         void _M_swap_data(_Deque_impl& __x) _GLIBCXX_NOEXCEPT
576         {
577           using std::swap;
578           swap(this->_M_start, __x._M_start);
579           swap(this->_M_finish, __x._M_finish);
580           swap(this->_M_map, __x._M_map);
581           swap(this->_M_map_size, __x._M_map_size);
582         }
583       };
584
585       _Tp_alloc_type&
586       _M_get_Tp_allocator() _GLIBCXX_NOEXCEPT
587       { return *static_cast<_Tp_alloc_type*>(&this->_M_impl); }
588
589       const _Tp_alloc_type&
590       _M_get_Tp_allocator() const _GLIBCXX_NOEXCEPT
591       { return *static_cast<const _Tp_alloc_type*>(&this->_M_impl); }
592
593       _Map_alloc_type
594       _M_get_map_allocator() const _GLIBCXX_NOEXCEPT
595       { return _Map_alloc_type(_M_get_Tp_allocator()); }
596
597       _Ptr
598       _M_allocate_node()
599       { 
600         typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
601         return _Traits::allocate(_M_impl, __deque_buf_size(sizeof(_Tp)));
602       }
603
604       void
605       _M_deallocate_node(_Ptr __p) _GLIBCXX_NOEXCEPT
606       {
607         typedef __gnu_cxx::__alloc_traits<_Tp_alloc_type> _Traits;
608         _Traits::deallocate(_M_impl, __p, __deque_buf_size(sizeof(_Tp)));
609       }
610
611       _Map_pointer
612       _M_allocate_map(size_t __n)
613       {
614         _Map_alloc_type __map_alloc = _M_get_map_allocator();
615         return _Map_alloc_traits::allocate(__map_alloc, __n);
616       }
617
618       void
619       _M_deallocate_map(_Map_pointer __p, size_t __n) _GLIBCXX_NOEXCEPT
620       {
621         _Map_alloc_type __map_alloc = _M_get_map_allocator();
622         _Map_alloc_traits::deallocate(__map_alloc, __p, __n);
623       }
624
625     protected:
626       void _M_initialize_map(size_t);
627       void _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish);
628       void _M_destroy_nodes(_Map_pointer __nstart,
629                             _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT;
630       enum { _S_initial_map_size = 8 };
631
632       _Deque_impl _M_impl;
633
634 #if __cplusplus >= 201103L
635     private:
636       _Deque_impl
637       _M_move_impl()
638       {
639         if (!_M_impl._M_map)
640           return std::move(_M_impl);
641
642         // Create a copy of the current allocator.
643         _Tp_alloc_type __alloc{_M_get_Tp_allocator()};
644         // Put that copy in a moved-from state.
645         _Tp_alloc_type __sink __attribute((__unused__)) {std::move(__alloc)};
646         // Create an empty map that allocates using the moved-from allocator.
647         _Deque_base __empty{__alloc};
648         // Now safe to modify current allocator and perform non-throwing swaps.
649         _Deque_impl __ret{std::move(_M_get_Tp_allocator())};
650         _M_impl._M_swap_data(__ret);
651         _M_impl._M_swap_data(__empty._M_impl);
652         return __ret;
653       }
654 #endif
655     };
656
657   template<typename _Tp, typename _Alloc>
658     _Deque_base<_Tp, _Alloc>::
659     ~_Deque_base() _GLIBCXX_NOEXCEPT
660     {
661       if (this->_M_impl._M_map)
662         {
663           _M_destroy_nodes(this->_M_impl._M_start._M_node,
664                            this->_M_impl._M_finish._M_node + 1);
665           _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
666         }
667     }
668
669   /**
670    *  @brief Layout storage.
671    *  @param  __num_elements  The count of T's for which to allocate space
672    *                          at first.
673    *  @return   Nothing.
674    *
675    *  The initial underlying memory layout is a bit complicated...
676   */
677   template<typename _Tp, typename _Alloc>
678     void
679     _Deque_base<_Tp, _Alloc>::
680     _M_initialize_map(size_t __num_elements)
681     {
682       const size_t __num_nodes = (__num_elements/ __deque_buf_size(sizeof(_Tp))
683                                   + 1);
684
685       this->_M_impl._M_map_size = std::max((size_t) _S_initial_map_size,
686                                            size_t(__num_nodes + 2));
687       this->_M_impl._M_map = _M_allocate_map(this->_M_impl._M_map_size);
688
689       // For "small" maps (needing less than _M_map_size nodes), allocation
690       // starts in the middle elements and grows outwards.  So nstart may be
691       // the beginning of _M_map, but for small maps it may be as far in as
692       // _M_map+3.
693
694       _Map_pointer __nstart = (this->_M_impl._M_map
695                                + (this->_M_impl._M_map_size - __num_nodes) / 2);
696       _Map_pointer __nfinish = __nstart + __num_nodes;
697
698       __try
699         { _M_create_nodes(__nstart, __nfinish); }
700       __catch(...)
701         {
702           _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
703           this->_M_impl._M_map = _Map_pointer();
704           this->_M_impl._M_map_size = 0;
705           __throw_exception_again;
706         }
707
708       this->_M_impl._M_start._M_set_node(__nstart);
709       this->_M_impl._M_finish._M_set_node(__nfinish - 1);
710       this->_M_impl._M_start._M_cur = _M_impl._M_start._M_first;
711       this->_M_impl._M_finish._M_cur = (this->_M_impl._M_finish._M_first
712                                         + __num_elements
713                                         % __deque_buf_size(sizeof(_Tp)));
714     }
715
716   template<typename _Tp, typename _Alloc>
717     void
718     _Deque_base<_Tp, _Alloc>::
719     _M_create_nodes(_Map_pointer __nstart, _Map_pointer __nfinish)
720     {
721       _Map_pointer __cur;
722       __try
723         {
724           for (__cur = __nstart; __cur < __nfinish; ++__cur)
725             *__cur = this->_M_allocate_node();
726         }
727       __catch(...)
728         {
729           _M_destroy_nodes(__nstart, __cur);
730           __throw_exception_again;
731         }
732     }
733
734   template<typename _Tp, typename _Alloc>
735     void
736     _Deque_base<_Tp, _Alloc>::
737     _M_destroy_nodes(_Map_pointer __nstart,
738                      _Map_pointer __nfinish) _GLIBCXX_NOEXCEPT
739     {
740       for (_Map_pointer __n = __nstart; __n < __nfinish; ++__n)
741         _M_deallocate_node(*__n);
742     }
743
744   /**
745    *  @brief  A standard container using fixed-size memory allocation and
746    *  constant-time manipulation of elements at either end.
747    *
748    *  @ingroup sequences
749    *
750    *  @tparam _Tp  Type of element.
751    *  @tparam _Alloc  Allocator type, defaults to allocator<_Tp>.
752    *
753    *  Meets the requirements of a <a href="tables.html#65">container</a>, a
754    *  <a href="tables.html#66">reversible container</a>, and a
755    *  <a href="tables.html#67">sequence</a>, including the
756    *  <a href="tables.html#68">optional sequence requirements</a>.
757    *
758    *  In previous HP/SGI versions of deque, there was an extra template
759    *  parameter so users could control the node size.  This extension turned
760    *  out to violate the C++ standard (it can be detected using template
761    *  template parameters), and it was removed.
762    *
763    *  Here's how a deque<Tp> manages memory.  Each deque has 4 members:
764    *
765    *  - Tp**        _M_map
766    *  - size_t      _M_map_size
767    *  - iterator    _M_start, _M_finish
768    *
769    *  map_size is at least 8.  %map is an array of map_size
770    *  pointers-to-@a nodes.  (The name %map has nothing to do with the
771    *  std::map class, and @b nodes should not be confused with
772    *  std::list's usage of @a node.)
773    *
774    *  A @a node has no specific type name as such, but it is referred
775    *  to as @a node in this file.  It is a simple array-of-Tp.  If Tp
776    *  is very large, there will be one Tp element per node (i.e., an
777    *  @a array of one).  For non-huge Tp's, node size is inversely
778    *  related to Tp size: the larger the Tp, the fewer Tp's will fit
779    *  in a node.  The goal here is to keep the total size of a node
780    *  relatively small and constant over different Tp's, to improve
781    *  allocator efficiency.
782    *
783    *  Not every pointer in the %map array will point to a node.  If
784    *  the initial number of elements in the deque is small, the
785    *  /middle/ %map pointers will be valid, and the ones at the edges
786    *  will be unused.  This same situation will arise as the %map
787    *  grows: available %map pointers, if any, will be on the ends.  As
788    *  new nodes are created, only a subset of the %map's pointers need
789    *  to be copied @a outward.
790    *
791    *  Class invariants:
792    * - For any nonsingular iterator i:
793    *    - i.node points to a member of the %map array.  (Yes, you read that
794    *      correctly:  i.node does not actually point to a node.)  The member of
795    *      the %map array is what actually points to the node.
796    *    - i.first == *(i.node)    (This points to the node (first Tp element).)
797    *    - i.last  == i.first + node_size
798    *    - i.cur is a pointer in the range [i.first, i.last).  NOTE:
799    *      the implication of this is that i.cur is always a dereferenceable
800    *      pointer, even if i is a past-the-end iterator.
801    * - Start and Finish are always nonsingular iterators.  NOTE: this
802    * means that an empty deque must have one node, a deque with <N
803    * elements (where N is the node buffer size) must have one node, a
804    * deque with N through (2N-1) elements must have two nodes, etc.
805    * - For every node other than start.node and finish.node, every
806    * element in the node is an initialized object.  If start.node ==
807    * finish.node, then [start.cur, finish.cur) are initialized
808    * objects, and the elements outside that range are uninitialized
809    * storage.  Otherwise, [start.cur, start.last) and [finish.first,
810    * finish.cur) are initialized objects, and [start.first, start.cur)
811    * and [finish.cur, finish.last) are uninitialized storage.
812    * - [%map, %map + map_size) is a valid, non-empty range.
813    * - [start.node, finish.node] is a valid range contained within
814    *   [%map, %map + map_size).
815    * - A pointer in the range [%map, %map + map_size) points to an allocated
816    *   node if and only if the pointer is in the range
817    *   [start.node, finish.node].
818    *
819    *  Here's the magic:  nothing in deque is @b aware of the discontiguous
820    *  storage!
821    *
822    *  The memory setup and layout occurs in the parent, _Base, and the iterator
823    *  class is entirely responsible for @a leaping from one node to the next.
824    *  All the implementation routines for deque itself work only through the
825    *  start and finish iterators.  This keeps the routines simple and sane,
826    *  and we can use other standard algorithms as well.
827   */
828   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
829     class deque : protected _Deque_base<_Tp, _Alloc>
830     {
831       // concept requirements
832       typedef typename _Alloc::value_type        _Alloc_value_type;
833       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
834       __glibcxx_class_requires2(_Tp, _Alloc_value_type, _SameTypeConcept)
835
836       typedef _Deque_base<_Tp, _Alloc>                  _Base;
837       typedef typename _Base::_Tp_alloc_type            _Tp_alloc_type;
838       typedef typename _Base::_Alloc_traits             _Alloc_traits;
839       typedef typename _Base::_Map_pointer              _Map_pointer;
840
841     public:
842       typedef _Tp                                        value_type;
843       typedef typename _Alloc_traits::pointer            pointer;
844       typedef typename _Alloc_traits::const_pointer      const_pointer;
845       typedef typename _Alloc_traits::reference          reference;
846       typedef typename _Alloc_traits::const_reference    const_reference;
847       typedef typename _Base::iterator                   iterator;
848       typedef typename _Base::const_iterator             const_iterator;
849       typedef std::reverse_iterator<const_iterator>      const_reverse_iterator;
850       typedef std::reverse_iterator<iterator>            reverse_iterator;
851       typedef size_t                             size_type;
852       typedef ptrdiff_t                          difference_type;
853       typedef _Alloc                             allocator_type;
854
855     protected:
856       static size_t _S_buffer_size() _GLIBCXX_NOEXCEPT
857       { return __deque_buf_size(sizeof(_Tp)); }
858
859       // Functions controlling memory layout, and nothing else.
860       using _Base::_M_initialize_map;
861       using _Base::_M_create_nodes;
862       using _Base::_M_destroy_nodes;
863       using _Base::_M_allocate_node;
864       using _Base::_M_deallocate_node;
865       using _Base::_M_allocate_map;
866       using _Base::_M_deallocate_map;
867       using _Base::_M_get_Tp_allocator;
868
869       /** 
870        *  A total of four data members accumulated down the hierarchy.
871        *  May be accessed via _M_impl.*
872        */
873       using _Base::_M_impl;
874
875     public:
876       // [23.2.1.1] construct/copy/destroy
877       // (assign() and get_allocator() are also listed in this section)
878
879       /**
880        *  @brief  Creates a %deque with no elements.
881        */
882       deque() : _Base() { }
883
884       /**
885        *  @brief  Creates a %deque with no elements.
886        *  @param  __a  An allocator object.
887        */
888       explicit
889       deque(const allocator_type& __a)
890       : _Base(__a, 0) { }
891
892 #if __cplusplus >= 201103L
893       /**
894        *  @brief  Creates a %deque with default constructed elements.
895        *  @param  __n  The number of elements to initially create.
896        *
897        *  This constructor fills the %deque with @a n default
898        *  constructed elements.
899        */
900       explicit
901       deque(size_type __n, const allocator_type& __a = allocator_type())
902       : _Base(__a, __n)
903       { _M_default_initialize(); }
904
905       /**
906        *  @brief  Creates a %deque with copies of an exemplar element.
907        *  @param  __n  The number of elements to initially create.
908        *  @param  __value  An element to copy.
909        *  @param  __a  An allocator.
910        *
911        *  This constructor fills the %deque with @a __n copies of @a __value.
912        */
913       deque(size_type __n, const value_type& __value,
914             const allocator_type& __a = allocator_type())
915       : _Base(__a, __n)
916       { _M_fill_initialize(__value); }
917 #else
918       /**
919        *  @brief  Creates a %deque with copies of an exemplar element.
920        *  @param  __n  The number of elements to initially create.
921        *  @param  __value  An element to copy.
922        *  @param  __a  An allocator.
923        *
924        *  This constructor fills the %deque with @a __n copies of @a __value.
925        */
926       explicit
927       deque(size_type __n, const value_type& __value = value_type(),
928             const allocator_type& __a = allocator_type())
929       : _Base(__a, __n)
930       { _M_fill_initialize(__value); }
931 #endif
932
933       /**
934        *  @brief  %Deque copy constructor.
935        *  @param  __x  A %deque of identical element and allocator types.
936        *
937        *  The newly-created %deque uses a copy of the allocation object used
938        *  by @a __x.
939        */
940       deque(const deque& __x)
941       : _Base(_Alloc_traits::_S_select_on_copy(__x._M_get_Tp_allocator()),
942               __x.size())
943       { std::__uninitialized_copy_a(__x.begin(), __x.end(), 
944                                     this->_M_impl._M_start,
945                                     _M_get_Tp_allocator()); }
946
947 #if __cplusplus >= 201103L
948       /**
949        *  @brief  %Deque move constructor.
950        *  @param  __x  A %deque of identical element and allocator types.
951        *
952        *  The newly-created %deque contains the exact contents of @a __x.
953        *  The contents of @a __x are a valid, but unspecified %deque.
954        */
955       deque(deque&& __x)
956       : _Base(std::move(__x)) { }
957
958       /// Copy constructor with alternative allocator
959       deque(const deque& __x, const allocator_type& __a)
960       : _Base(__a, __x.size())
961       { std::__uninitialized_copy_a(__x.begin(), __x.end(),
962                                     this->_M_impl._M_start,
963                                     _M_get_Tp_allocator()); }
964
965       /// Move constructor with alternative allocator
966       deque(deque&& __x, const allocator_type& __a)
967       : _Base(std::move(__x), __a, __x.size())
968       {
969         if (__x.get_allocator() != __a)
970           {
971             std::__uninitialized_move_a(__x.begin(), __x.end(),
972                                         this->_M_impl._M_start,
973                                         _M_get_Tp_allocator());
974             __x.clear();
975           }
976       }
977
978       /**
979        *  @brief  Builds a %deque from an initializer list.
980        *  @param  __l  An initializer_list.
981        *  @param  __a  An allocator object.
982        *
983        *  Create a %deque consisting of copies of the elements in the
984        *  initializer_list @a __l.
985        *
986        *  This will call the element type's copy constructor N times
987        *  (where N is __l.size()) and do no memory reallocation.
988        */
989       deque(initializer_list<value_type> __l,
990             const allocator_type& __a = allocator_type())
991       : _Base(__a)
992       {
993         _M_range_initialize(__l.begin(), __l.end(),
994                             random_access_iterator_tag());
995       }
996 #endif
997
998       /**
999        *  @brief  Builds a %deque from a range.
1000        *  @param  __first  An input iterator.
1001        *  @param  __last  An input iterator.
1002        *  @param  __a  An allocator object.
1003        *
1004        *  Create a %deque consisting of copies of the elements from [__first,
1005        *  __last).
1006        *
1007        *  If the iterators are forward, bidirectional, or random-access, then
1008        *  this will call the elements' copy constructor N times (where N is
1009        *  distance(__first,__last)) and do no memory reallocation.  But if only
1010        *  input iterators are used, then this will do at most 2N calls to the
1011        *  copy constructor, and logN memory reallocations.
1012        */
1013 #if __cplusplus >= 201103L
1014       template<typename _InputIterator,
1015                typename = std::_RequireInputIter<_InputIterator>>
1016         deque(_InputIterator __first, _InputIterator __last,
1017               const allocator_type& __a = allocator_type())
1018         : _Base(__a)
1019         { _M_initialize_dispatch(__first, __last, __false_type()); }
1020 #else
1021       template<typename _InputIterator>
1022         deque(_InputIterator __first, _InputIterator __last,
1023               const allocator_type& __a = allocator_type())
1024         : _Base(__a)
1025         {
1026           // Check whether it's an integral type.  If so, it's not an iterator.
1027           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1028           _M_initialize_dispatch(__first, __last, _Integral());
1029         }
1030 #endif
1031
1032       /**
1033        *  The dtor only erases the elements, and note that if the elements
1034        *  themselves are pointers, the pointed-to memory is not touched in any
1035        *  way.  Managing the pointer is the user's responsibility.
1036        */
1037       ~deque()
1038       { _M_destroy_data(begin(), end(), _M_get_Tp_allocator()); }
1039
1040       /**
1041        *  @brief  %Deque assignment operator.
1042        *  @param  __x  A %deque of identical element and allocator types.
1043        *
1044        *  All the elements of @a x are copied, but unlike the copy constructor,
1045        *  the allocator object is not copied.
1046        */
1047       deque&
1048       operator=(const deque& __x);
1049
1050 #if __cplusplus >= 201103L
1051       /**
1052        *  @brief  %Deque move assignment operator.
1053        *  @param  __x  A %deque of identical element and allocator types.
1054        *
1055        *  The contents of @a __x are moved into this deque (without copying,
1056        *  if the allocators permit it).
1057        *  @a __x is a valid, but unspecified %deque.
1058        */
1059       deque&
1060       operator=(deque&& __x) noexcept(_Alloc_traits::_S_always_equal())
1061       {
1062         constexpr bool __always_equal = _Alloc_traits::_S_always_equal();
1063         _M_move_assign1(std::move(__x),
1064                         integral_constant<bool, __always_equal>());
1065         return *this;
1066       }
1067
1068       /**
1069        *  @brief  Assigns an initializer list to a %deque.
1070        *  @param  __l  An initializer_list.
1071        *
1072        *  This function fills a %deque with copies of the elements in the
1073        *  initializer_list @a __l.
1074        *
1075        *  Note that the assignment completely changes the %deque and that the
1076        *  resulting %deque's size is the same as the number of elements
1077        *  assigned.  Old data may be lost.
1078        */
1079       deque&
1080       operator=(initializer_list<value_type> __l)
1081       {
1082         this->assign(__l.begin(), __l.end());
1083         return *this;
1084       }
1085 #endif
1086
1087       /**
1088        *  @brief  Assigns a given value to a %deque.
1089        *  @param  __n  Number of elements to be assigned.
1090        *  @param  __val  Value to be assigned.
1091        *
1092        *  This function fills a %deque with @a n copies of the given
1093        *  value.  Note that the assignment completely changes the
1094        *  %deque and that the resulting %deque's size is the same as
1095        *  the number of elements assigned.  Old data may be lost.
1096        */
1097       void
1098       assign(size_type __n, const value_type& __val)
1099       { _M_fill_assign(__n, __val); }
1100
1101       /**
1102        *  @brief  Assigns a range to a %deque.
1103        *  @param  __first  An input iterator.
1104        *  @param  __last   An input iterator.
1105        *
1106        *  This function fills a %deque with copies of the elements in the
1107        *  range [__first,__last).
1108        *
1109        *  Note that the assignment completely changes the %deque and that the
1110        *  resulting %deque's size is the same as the number of elements
1111        *  assigned.  Old data may be lost.
1112        */
1113 #if __cplusplus >= 201103L
1114       template<typename _InputIterator,
1115                typename = std::_RequireInputIter<_InputIterator>>
1116         void
1117         assign(_InputIterator __first, _InputIterator __last)
1118         { _M_assign_dispatch(__first, __last, __false_type()); }
1119 #else
1120       template<typename _InputIterator>
1121         void
1122         assign(_InputIterator __first, _InputIterator __last)
1123         {
1124           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1125           _M_assign_dispatch(__first, __last, _Integral());
1126         }
1127 #endif
1128
1129 #if __cplusplus >= 201103L
1130       /**
1131        *  @brief  Assigns an initializer list to a %deque.
1132        *  @param  __l  An initializer_list.
1133        *
1134        *  This function fills a %deque with copies of the elements in the
1135        *  initializer_list @a __l.
1136        *
1137        *  Note that the assignment completely changes the %deque and that the
1138        *  resulting %deque's size is the same as the number of elements
1139        *  assigned.  Old data may be lost.
1140        */
1141       void
1142       assign(initializer_list<value_type> __l)
1143       { this->assign(__l.begin(), __l.end()); }
1144 #endif
1145
1146       /// Get a copy of the memory allocation object.
1147       allocator_type
1148       get_allocator() const _GLIBCXX_NOEXCEPT
1149       { return _Base::get_allocator(); }
1150
1151       // iterators
1152       /**
1153        *  Returns a read/write iterator that points to the first element in the
1154        *  %deque.  Iteration is done in ordinary element order.
1155        */
1156       iterator
1157       begin() _GLIBCXX_NOEXCEPT
1158       { return this->_M_impl._M_start; }
1159
1160       /**
1161        *  Returns a read-only (constant) iterator that points to the first
1162        *  element in the %deque.  Iteration is done in ordinary element order.
1163        */
1164       const_iterator
1165       begin() const _GLIBCXX_NOEXCEPT
1166       { return this->_M_impl._M_start; }
1167
1168       /**
1169        *  Returns a read/write iterator that points one past the last
1170        *  element in the %deque.  Iteration is done in ordinary
1171        *  element order.
1172        */
1173       iterator
1174       end() _GLIBCXX_NOEXCEPT
1175       { return this->_M_impl._M_finish; }
1176
1177       /**
1178        *  Returns a read-only (constant) iterator that points one past
1179        *  the last element in the %deque.  Iteration is done in
1180        *  ordinary element order.
1181        */
1182       const_iterator
1183       end() const _GLIBCXX_NOEXCEPT
1184       { return this->_M_impl._M_finish; }
1185
1186       /**
1187        *  Returns a read/write reverse iterator that points to the
1188        *  last element in the %deque.  Iteration is done in reverse
1189        *  element order.
1190        */
1191       reverse_iterator
1192       rbegin() _GLIBCXX_NOEXCEPT
1193       { return reverse_iterator(this->_M_impl._M_finish); }
1194
1195       /**
1196        *  Returns a read-only (constant) reverse iterator that points
1197        *  to the last element in the %deque.  Iteration is done in
1198        *  reverse element order.
1199        */
1200       const_reverse_iterator
1201       rbegin() const _GLIBCXX_NOEXCEPT
1202       { return const_reverse_iterator(this->_M_impl._M_finish); }
1203
1204       /**
1205        *  Returns a read/write reverse iterator that points to one
1206        *  before the first element in the %deque.  Iteration is done
1207        *  in reverse element order.
1208        */
1209       reverse_iterator
1210       rend() _GLIBCXX_NOEXCEPT
1211       { return reverse_iterator(this->_M_impl._M_start); }
1212
1213       /**
1214        *  Returns a read-only (constant) reverse iterator that points
1215        *  to one before the first element in the %deque.  Iteration is
1216        *  done in reverse element order.
1217        */
1218       const_reverse_iterator
1219       rend() const _GLIBCXX_NOEXCEPT
1220       { return const_reverse_iterator(this->_M_impl._M_start); }
1221
1222 #if __cplusplus >= 201103L
1223       /**
1224        *  Returns a read-only (constant) iterator that points to the first
1225        *  element in the %deque.  Iteration is done in ordinary element order.
1226        */
1227       const_iterator
1228       cbegin() const noexcept
1229       { return this->_M_impl._M_start; }
1230
1231       /**
1232        *  Returns a read-only (constant) iterator that points one past
1233        *  the last element in the %deque.  Iteration is done in
1234        *  ordinary element order.
1235        */
1236       const_iterator
1237       cend() const noexcept
1238       { return this->_M_impl._M_finish; }
1239
1240       /**
1241        *  Returns a read-only (constant) reverse iterator that points
1242        *  to the last element in the %deque.  Iteration is done in
1243        *  reverse element order.
1244        */
1245       const_reverse_iterator
1246       crbegin() const noexcept
1247       { return const_reverse_iterator(this->_M_impl._M_finish); }
1248
1249       /**
1250        *  Returns a read-only (constant) reverse iterator that points
1251        *  to one before the first element in the %deque.  Iteration is
1252        *  done in reverse element order.
1253        */
1254       const_reverse_iterator
1255       crend() const noexcept
1256       { return const_reverse_iterator(this->_M_impl._M_start); }
1257 #endif
1258
1259       // [23.2.1.2] capacity
1260       /**  Returns the number of elements in the %deque.  */
1261       size_type
1262       size() const _GLIBCXX_NOEXCEPT
1263       { return this->_M_impl._M_finish - this->_M_impl._M_start; }
1264
1265       /**  Returns the size() of the largest possible %deque.  */
1266       size_type
1267       max_size() const _GLIBCXX_NOEXCEPT
1268       { return _Alloc_traits::max_size(_M_get_Tp_allocator()); }
1269
1270 #if __cplusplus >= 201103L
1271       /**
1272        *  @brief  Resizes the %deque to the specified number of elements.
1273        *  @param  __new_size  Number of elements the %deque should contain.
1274        *
1275        *  This function will %resize the %deque to the specified
1276        *  number of elements.  If the number is smaller than the
1277        *  %deque's current size the %deque is truncated, otherwise
1278        *  default constructed elements are appended.
1279        */
1280       void
1281       resize(size_type __new_size)
1282       {
1283         const size_type __len = size();
1284         if (__new_size > __len)
1285           _M_default_append(__new_size - __len);
1286         else if (__new_size < __len)
1287           _M_erase_at_end(this->_M_impl._M_start
1288                           + difference_type(__new_size));
1289       }
1290
1291       /**
1292        *  @brief  Resizes the %deque to the specified number of elements.
1293        *  @param  __new_size  Number of elements the %deque should contain.
1294        *  @param  __x  Data with which new elements should be populated.
1295        *
1296        *  This function will %resize the %deque to the specified
1297        *  number of elements.  If the number is smaller than the
1298        *  %deque's current size the %deque is truncated, otherwise the
1299        *  %deque is extended and new elements are populated with given
1300        *  data.
1301        */
1302       void
1303       resize(size_type __new_size, const value_type& __x)
1304       {
1305         const size_type __len = size();
1306         if (__new_size > __len)
1307           insert(this->_M_impl._M_finish, __new_size - __len, __x);
1308         else if (__new_size < __len)
1309           _M_erase_at_end(this->_M_impl._M_start
1310                           + difference_type(__new_size));
1311       }
1312 #else
1313       /**
1314        *  @brief  Resizes the %deque to the specified number of elements.
1315        *  @param  __new_size  Number of elements the %deque should contain.
1316        *  @param  __x  Data with which new elements should be populated.
1317        *
1318        *  This function will %resize the %deque to the specified
1319        *  number of elements.  If the number is smaller than the
1320        *  %deque's current size the %deque is truncated, otherwise the
1321        *  %deque is extended and new elements are populated with given
1322        *  data.
1323        */
1324       void
1325       resize(size_type __new_size, value_type __x = value_type())
1326       {
1327         const size_type __len = size();
1328         if (__new_size > __len)
1329           insert(this->_M_impl._M_finish, __new_size - __len, __x);
1330         else if (__new_size < __len)
1331           _M_erase_at_end(this->_M_impl._M_start
1332                           + difference_type(__new_size));
1333       }
1334 #endif
1335
1336 #if __cplusplus >= 201103L
1337       /**  A non-binding request to reduce memory use.  */
1338       void
1339       shrink_to_fit() noexcept
1340       { _M_shrink_to_fit(); }
1341 #endif
1342
1343       /**
1344        *  Returns true if the %deque is empty.  (Thus begin() would
1345        *  equal end().)
1346        */
1347       bool
1348       empty() const _GLIBCXX_NOEXCEPT
1349       { return this->_M_impl._M_finish == this->_M_impl._M_start; }
1350
1351       // element access
1352       /**
1353        *  @brief Subscript access to the data contained in the %deque.
1354        *  @param __n The index of the element for which data should be
1355        *  accessed.
1356        *  @return  Read/write reference to data.
1357        *
1358        *  This operator allows for easy, array-style, data access.
1359        *  Note that data access with this operator is unchecked and
1360        *  out_of_range lookups are not defined. (For checked lookups
1361        *  see at().)
1362        */
1363       reference
1364       operator[](size_type __n) _GLIBCXX_NOEXCEPT
1365       { return this->_M_impl._M_start[difference_type(__n)]; }
1366
1367       /**
1368        *  @brief Subscript access to the data contained in the %deque.
1369        *  @param __n The index of the element for which data should be
1370        *  accessed.
1371        *  @return  Read-only (constant) reference to data.
1372        *
1373        *  This operator allows for easy, array-style, data access.
1374        *  Note that data access with this operator is unchecked and
1375        *  out_of_range lookups are not defined. (For checked lookups
1376        *  see at().)
1377        */
1378       const_reference
1379       operator[](size_type __n) const _GLIBCXX_NOEXCEPT
1380       { return this->_M_impl._M_start[difference_type(__n)]; }
1381
1382     protected:
1383       /// Safety check used only from at().
1384       void
1385       _M_range_check(size_type __n) const
1386       {
1387         if (__n >= this->size())
1388           __throw_out_of_range_fmt(__N("deque::_M_range_check: __n "
1389                                        "(which is %zu)>= this->size() "
1390                                        "(which is %zu)"),
1391                                    __n, this->size());
1392       }
1393
1394     public:
1395       /**
1396        *  @brief  Provides access to the data contained in the %deque.
1397        *  @param __n The index of the element for which data should be
1398        *  accessed.
1399        *  @return  Read/write reference to data.
1400        *  @throw  std::out_of_range  If @a __n is an invalid index.
1401        *
1402        *  This function provides for safer data access.  The parameter
1403        *  is first checked that it is in the range of the deque.  The
1404        *  function throws out_of_range if the check fails.
1405        */
1406       reference
1407       at(size_type __n)
1408       {
1409         _M_range_check(__n);
1410         return (*this)[__n];
1411       }
1412
1413       /**
1414        *  @brief  Provides access to the data contained in the %deque.
1415        *  @param __n The index of the element for which data should be
1416        *  accessed.
1417        *  @return  Read-only (constant) reference to data.
1418        *  @throw  std::out_of_range  If @a __n is an invalid index.
1419        *
1420        *  This function provides for safer data access.  The parameter is first
1421        *  checked that it is in the range of the deque.  The function throws
1422        *  out_of_range if the check fails.
1423        */
1424       const_reference
1425       at(size_type __n) const
1426       {
1427         _M_range_check(__n);
1428         return (*this)[__n];
1429       }
1430
1431       /**
1432        *  Returns a read/write reference to the data at the first
1433        *  element of the %deque.
1434        */
1435       reference
1436       front() _GLIBCXX_NOEXCEPT
1437       { return *begin(); }
1438
1439       /**
1440        *  Returns a read-only (constant) reference to the data at the first
1441        *  element of the %deque.
1442        */
1443       const_reference
1444       front() const _GLIBCXX_NOEXCEPT
1445       { return *begin(); }
1446
1447       /**
1448        *  Returns a read/write reference to the data at the last element of the
1449        *  %deque.
1450        */
1451       reference
1452       back() _GLIBCXX_NOEXCEPT
1453       {
1454         iterator __tmp = end();
1455         --__tmp;
1456         return *__tmp;
1457       }
1458
1459       /**
1460        *  Returns a read-only (constant) reference to the data at the last
1461        *  element of the %deque.
1462        */
1463       const_reference
1464       back() const _GLIBCXX_NOEXCEPT
1465       {
1466         const_iterator __tmp = end();
1467         --__tmp;
1468         return *__tmp;
1469       }
1470
1471       // [23.2.1.2] modifiers
1472       /**
1473        *  @brief  Add data to the front of the %deque.
1474        *  @param  __x  Data to be added.
1475        *
1476        *  This is a typical stack operation.  The function creates an
1477        *  element at the front of the %deque and assigns the given
1478        *  data to it.  Due to the nature of a %deque this operation
1479        *  can be done in constant time.
1480        */
1481       void
1482       push_front(const value_type& __x)
1483       {
1484         if (this->_M_impl._M_start._M_cur != this->_M_impl._M_start._M_first)
1485           {
1486             _Alloc_traits::construct(this->_M_impl,
1487                                      this->_M_impl._M_start._M_cur - 1,
1488                                      __x);
1489             --this->_M_impl._M_start._M_cur;
1490           }
1491         else
1492           _M_push_front_aux(__x);
1493       }
1494
1495 #if __cplusplus >= 201103L
1496       void
1497       push_front(value_type&& __x)
1498       { emplace_front(std::move(__x)); }
1499
1500       template<typename... _Args>
1501         void
1502         emplace_front(_Args&&... __args);
1503 #endif
1504
1505       /**
1506        *  @brief  Add data to the end of the %deque.
1507        *  @param  __x  Data to be added.
1508        *
1509        *  This is a typical stack operation.  The function creates an
1510        *  element at the end of the %deque and assigns the given data
1511        *  to it.  Due to the nature of a %deque this operation can be
1512        *  done in constant time.
1513        */
1514       void
1515       push_back(const value_type& __x)
1516       {
1517         if (this->_M_impl._M_finish._M_cur
1518             != this->_M_impl._M_finish._M_last - 1)
1519           {
1520             _Alloc_traits::construct(this->_M_impl,
1521                                      this->_M_impl._M_finish._M_cur, __x);
1522             ++this->_M_impl._M_finish._M_cur;
1523           }
1524         else
1525           _M_push_back_aux(__x);
1526       }
1527
1528 #if __cplusplus >= 201103L
1529       void
1530       push_back(value_type&& __x)
1531       { emplace_back(std::move(__x)); }
1532
1533       template<typename... _Args>
1534         void
1535         emplace_back(_Args&&... __args);
1536 #endif
1537
1538       /**
1539        *  @brief  Removes first element.
1540        *
1541        *  This is a typical stack operation.  It shrinks the %deque by one.
1542        *
1543        *  Note that no data is returned, and if the first element's data is
1544        *  needed, it should be retrieved before pop_front() is called.
1545        */
1546       void
1547       pop_front() _GLIBCXX_NOEXCEPT
1548       {
1549         if (this->_M_impl._M_start._M_cur
1550             != this->_M_impl._M_start._M_last - 1)
1551           {
1552             _Alloc_traits::destroy(this->_M_impl,
1553                                    this->_M_impl._M_start._M_cur);
1554             ++this->_M_impl._M_start._M_cur;
1555           }
1556         else
1557           _M_pop_front_aux();
1558       }
1559
1560       /**
1561        *  @brief  Removes last element.
1562        *
1563        *  This is a typical stack operation.  It shrinks the %deque by one.
1564        *
1565        *  Note that no data is returned, and if the last element's data is
1566        *  needed, it should be retrieved before pop_back() is called.
1567        */
1568       void
1569       pop_back() _GLIBCXX_NOEXCEPT
1570       {
1571         if (this->_M_impl._M_finish._M_cur
1572             != this->_M_impl._M_finish._M_first)
1573           {
1574             --this->_M_impl._M_finish._M_cur;
1575             _Alloc_traits::destroy(this->_M_impl,
1576                                    this->_M_impl._M_finish._M_cur);
1577           }
1578         else
1579           _M_pop_back_aux();
1580       }
1581
1582 #if __cplusplus >= 201103L
1583       /**
1584        *  @brief  Inserts an object in %deque before specified iterator.
1585        *  @param  __position  A const_iterator into the %deque.
1586        *  @param  __args  Arguments.
1587        *  @return  An iterator that points to the inserted data.
1588        *
1589        *  This function will insert an object of type T constructed
1590        *  with T(std::forward<Args>(args)...) before the specified location.
1591        */
1592       template<typename... _Args>
1593         iterator
1594         emplace(const_iterator __position, _Args&&... __args);
1595
1596       /**
1597        *  @brief  Inserts given value into %deque before specified iterator.
1598        *  @param  __position  A const_iterator into the %deque.
1599        *  @param  __x  Data to be inserted.
1600        *  @return  An iterator that points to the inserted data.
1601        *
1602        *  This function will insert a copy of the given value before the
1603        *  specified location.
1604        */
1605       iterator
1606       insert(const_iterator __position, const value_type& __x);
1607 #else
1608       /**
1609        *  @brief  Inserts given value into %deque before specified iterator.
1610        *  @param  __position  An iterator into the %deque.
1611        *  @param  __x  Data to be inserted.
1612        *  @return  An iterator that points to the inserted data.
1613        *
1614        *  This function will insert a copy of the given value before the
1615        *  specified location.
1616        */
1617       iterator
1618       insert(iterator __position, const value_type& __x);
1619 #endif
1620
1621 #if __cplusplus >= 201103L
1622       /**
1623        *  @brief  Inserts given rvalue into %deque before specified iterator.
1624        *  @param  __position  A const_iterator into the %deque.
1625        *  @param  __x  Data to be inserted.
1626        *  @return  An iterator that points to the inserted data.
1627        *
1628        *  This function will insert a copy of the given rvalue before the
1629        *  specified location.
1630        */
1631       iterator
1632       insert(const_iterator __position, value_type&& __x)
1633       { return emplace(__position, std::move(__x)); }
1634
1635       /**
1636        *  @brief  Inserts an initializer list into the %deque.
1637        *  @param  __p  An iterator into the %deque.
1638        *  @param  __l  An initializer_list.
1639        *
1640        *  This function will insert copies of the data in the
1641        *  initializer_list @a __l into the %deque before the location
1642        *  specified by @a __p.  This is known as <em>list insert</em>.
1643        */
1644       iterator
1645       insert(const_iterator __p, initializer_list<value_type> __l)
1646       { return this->insert(__p, __l.begin(), __l.end()); }
1647 #endif
1648
1649 #if __cplusplus >= 201103L
1650       /**
1651        *  @brief  Inserts a number of copies of given data into the %deque.
1652        *  @param  __position  A const_iterator into the %deque.
1653        *  @param  __n  Number of elements to be inserted.
1654        *  @param  __x  Data to be inserted.
1655        *  @return  An iterator that points to the inserted data.
1656        *
1657        *  This function will insert a specified number of copies of the given
1658        *  data before the location specified by @a __position.
1659        */
1660       iterator
1661       insert(const_iterator __position, size_type __n, const value_type& __x)
1662       {
1663         difference_type __offset = __position - cbegin();
1664         _M_fill_insert(__position._M_const_cast(), __n, __x);
1665         return begin() + __offset;
1666       }
1667 #else
1668       /**
1669        *  @brief  Inserts a number of copies of given data into the %deque.
1670        *  @param  __position  An iterator into the %deque.
1671        *  @param  __n  Number of elements to be inserted.
1672        *  @param  __x  Data to be inserted.
1673        *
1674        *  This function will insert a specified number of copies of the given
1675        *  data before the location specified by @a __position.
1676        */
1677       void
1678       insert(iterator __position, size_type __n, const value_type& __x)
1679       { _M_fill_insert(__position, __n, __x); }
1680 #endif
1681
1682 #if __cplusplus >= 201103L
1683       /**
1684        *  @brief  Inserts a range into the %deque.
1685        *  @param  __position  A const_iterator into the %deque.
1686        *  @param  __first  An input iterator.
1687        *  @param  __last   An input iterator.
1688        *  @return  An iterator that points to the inserted data.
1689        *
1690        *  This function will insert copies of the data in the range
1691        *  [__first,__last) into the %deque before the location specified
1692        *  by @a __position.  This is known as <em>range insert</em>.
1693        */
1694       template<typename _InputIterator,
1695                typename = std::_RequireInputIter<_InputIterator>>
1696         iterator
1697         insert(const_iterator __position, _InputIterator __first,
1698                _InputIterator __last)
1699         {
1700           difference_type __offset = __position - cbegin();
1701           _M_insert_dispatch(__position._M_const_cast(),
1702                              __first, __last, __false_type());
1703           return begin() + __offset;
1704         }
1705 #else
1706       /**
1707        *  @brief  Inserts a range into the %deque.
1708        *  @param  __position  An iterator into the %deque.
1709        *  @param  __first  An input iterator.
1710        *  @param  __last   An input iterator.
1711        *
1712        *  This function will insert copies of the data in the range
1713        *  [__first,__last) into the %deque before the location specified
1714        *  by @a __position.  This is known as <em>range insert</em>.
1715        */
1716       template<typename _InputIterator>
1717         void
1718         insert(iterator __position, _InputIterator __first,
1719                _InputIterator __last)
1720         {
1721           // Check whether it's an integral type.  If so, it's not an iterator.
1722           typedef typename std::__is_integer<_InputIterator>::__type _Integral;
1723           _M_insert_dispatch(__position, __first, __last, _Integral());
1724         }
1725 #endif
1726
1727       /**
1728        *  @brief  Remove element at given position.
1729        *  @param  __position  Iterator pointing to element to be erased.
1730        *  @return  An iterator pointing to the next element (or end()).
1731        *
1732        *  This function will erase the element at the given position and thus
1733        *  shorten the %deque by one.
1734        *
1735        *  The user is cautioned that
1736        *  this function only erases the element, and that if the element is
1737        *  itself a pointer, the pointed-to memory is not touched in any way.
1738        *  Managing the pointer is the user's responsibility.
1739        */
1740       iterator
1741 #if __cplusplus >= 201103L
1742       erase(const_iterator __position)
1743 #else
1744       erase(iterator __position)
1745 #endif
1746       { return _M_erase(__position._M_const_cast()); }
1747
1748       /**
1749        *  @brief  Remove a range of elements.
1750        *  @param  __first  Iterator pointing to the first element to be erased.
1751        *  @param  __last  Iterator pointing to one past the last element to be
1752        *                erased.
1753        *  @return  An iterator pointing to the element pointed to by @a last
1754        *           prior to erasing (or end()).
1755        *
1756        *  This function will erase the elements in the range
1757        *  [__first,__last) and shorten the %deque accordingly.
1758        *
1759        *  The user is cautioned that
1760        *  this function only erases the elements, and that if the elements
1761        *  themselves are pointers, the pointed-to memory is not touched in any
1762        *  way.  Managing the pointer is the user's responsibility.
1763        */
1764       iterator
1765 #if __cplusplus >= 201103L
1766       erase(const_iterator __first, const_iterator __last)
1767 #else
1768       erase(iterator __first, iterator __last)
1769 #endif
1770       { return _M_erase(__first._M_const_cast(), __last._M_const_cast()); }
1771
1772       /**
1773        *  @brief  Swaps data with another %deque.
1774        *  @param  __x  A %deque of the same element and allocator types.
1775        *
1776        *  This exchanges the elements between two deques in constant time.
1777        *  (Four pointers, so it should be quite fast.)
1778        *  Note that the global std::swap() function is specialized such that
1779        *  std::swap(d1,d2) will feed to this function.
1780        */
1781       void
1782       swap(deque& __x)
1783 #if __cplusplus >= 201103L
1784       noexcept(_Alloc_traits::_S_nothrow_swap())
1785 #endif
1786       {
1787         _M_impl._M_swap_data(__x._M_impl);
1788         _Alloc_traits::_S_on_swap(_M_get_Tp_allocator(),
1789                                   __x._M_get_Tp_allocator());
1790       }
1791
1792       /**
1793        *  Erases all the elements.  Note that this function only erases the
1794        *  elements, and that if the elements themselves are pointers, the
1795        *  pointed-to memory is not touched in any way.  Managing the pointer is
1796        *  the user's responsibility.
1797        */
1798       void
1799       clear() _GLIBCXX_NOEXCEPT
1800       { _M_erase_at_end(begin()); }
1801
1802     protected:
1803       // Internal constructor functions follow.
1804
1805       // called by the range constructor to implement [23.1.1]/9
1806
1807       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1808       // 438. Ambiguity in the "do the right thing" clause
1809       template<typename _Integer>
1810         void
1811         _M_initialize_dispatch(_Integer __n, _Integer __x, __true_type)
1812         {
1813           _M_initialize_map(static_cast<size_type>(__n));
1814           _M_fill_initialize(__x);
1815         }
1816
1817       // called by the range constructor to implement [23.1.1]/9
1818       template<typename _InputIterator>
1819         void
1820         _M_initialize_dispatch(_InputIterator __first, _InputIterator __last,
1821                                __false_type)
1822         {
1823           typedef typename std::iterator_traits<_InputIterator>::
1824             iterator_category _IterCategory;
1825           _M_range_initialize(__first, __last, _IterCategory());
1826         }
1827
1828       // called by the second initialize_dispatch above
1829       //@{
1830       /**
1831        *  @brief Fills the deque with whatever is in [first,last).
1832        *  @param  __first  An input iterator.
1833        *  @param  __last  An input iterator.
1834        *  @return   Nothing.
1835        *
1836        *  If the iterators are actually forward iterators (or better), then the
1837        *  memory layout can be done all at once.  Else we move forward using
1838        *  push_back on each value from the iterator.
1839        */
1840       template<typename _InputIterator>
1841         void
1842         _M_range_initialize(_InputIterator __first, _InputIterator __last,
1843                             std::input_iterator_tag);
1844
1845       // called by the second initialize_dispatch above
1846       template<typename _ForwardIterator>
1847         void
1848         _M_range_initialize(_ForwardIterator __first, _ForwardIterator __last,
1849                             std::forward_iterator_tag);
1850       //@}
1851
1852       /**
1853        *  @brief Fills the %deque with copies of value.
1854        *  @param  __value  Initial value.
1855        *  @return   Nothing.
1856        *  @pre _M_start and _M_finish have already been initialized,
1857        *  but none of the %deque's elements have yet been constructed.
1858        *
1859        *  This function is called only when the user provides an explicit size
1860        *  (with or without an explicit exemplar value).
1861        */
1862       void
1863       _M_fill_initialize(const value_type& __value);
1864
1865 #if __cplusplus >= 201103L
1866       // called by deque(n).
1867       void
1868       _M_default_initialize();
1869 #endif
1870
1871       // Internal assign functions follow.  The *_aux functions do the actual
1872       // assignment work for the range versions.
1873
1874       // called by the range assign to implement [23.1.1]/9
1875
1876       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1877       // 438. Ambiguity in the "do the right thing" clause
1878       template<typename _Integer>
1879         void
1880         _M_assign_dispatch(_Integer __n, _Integer __val, __true_type)
1881         { _M_fill_assign(__n, __val); }
1882
1883       // called by the range assign to implement [23.1.1]/9
1884       template<typename _InputIterator>
1885         void
1886         _M_assign_dispatch(_InputIterator __first, _InputIterator __last,
1887                            __false_type)
1888         {
1889           typedef typename std::iterator_traits<_InputIterator>::
1890             iterator_category _IterCategory;
1891           _M_assign_aux(__first, __last, _IterCategory());
1892         }
1893
1894       // called by the second assign_dispatch above
1895       template<typename _InputIterator>
1896         void
1897         _M_assign_aux(_InputIterator __first, _InputIterator __last,
1898                       std::input_iterator_tag);
1899
1900       // called by the second assign_dispatch above
1901       template<typename _ForwardIterator>
1902         void
1903         _M_assign_aux(_ForwardIterator __first, _ForwardIterator __last,
1904                       std::forward_iterator_tag)
1905         {
1906           const size_type __len = std::distance(__first, __last);
1907           if (__len > size())
1908             {
1909               _ForwardIterator __mid = __first;
1910               std::advance(__mid, size());
1911               std::copy(__first, __mid, begin());
1912               insert(end(), __mid, __last);
1913             }
1914           else
1915             _M_erase_at_end(std::copy(__first, __last, begin()));
1916         }
1917
1918       // Called by assign(n,t), and the range assign when it turns out
1919       // to be the same thing.
1920       void
1921       _M_fill_assign(size_type __n, const value_type& __val)
1922       {
1923         if (__n > size())
1924           {
1925             std::fill(begin(), end(), __val);
1926             insert(end(), __n - size(), __val);
1927           }
1928         else
1929           {
1930             _M_erase_at_end(begin() + difference_type(__n));
1931             std::fill(begin(), end(), __val);
1932           }
1933       }
1934
1935       //@{
1936       /// Helper functions for push_* and pop_*.
1937 #if __cplusplus < 201103L
1938       void _M_push_back_aux(const value_type&);
1939
1940       void _M_push_front_aux(const value_type&);
1941 #else
1942       template<typename... _Args>
1943         void _M_push_back_aux(_Args&&... __args);
1944
1945       template<typename... _Args>
1946         void _M_push_front_aux(_Args&&... __args);
1947 #endif
1948
1949       void _M_pop_back_aux();
1950
1951       void _M_pop_front_aux();
1952       //@}
1953
1954       // Internal insert functions follow.  The *_aux functions do the actual
1955       // insertion work when all shortcuts fail.
1956
1957       // called by the range insert to implement [23.1.1]/9
1958
1959       // _GLIBCXX_RESOLVE_LIB_DEFECTS
1960       // 438. Ambiguity in the "do the right thing" clause
1961       template<typename _Integer>
1962         void
1963         _M_insert_dispatch(iterator __pos,
1964                            _Integer __n, _Integer __x, __true_type)
1965         { _M_fill_insert(__pos, __n, __x); }
1966
1967       // called by the range insert to implement [23.1.1]/9
1968       template<typename _InputIterator>
1969         void
1970         _M_insert_dispatch(iterator __pos,
1971                            _InputIterator __first, _InputIterator __last,
1972                            __false_type)
1973         {
1974           typedef typename std::iterator_traits<_InputIterator>::
1975             iterator_category _IterCategory;
1976           _M_range_insert_aux(__pos, __first, __last, _IterCategory());
1977         }
1978
1979       // called by the second insert_dispatch above
1980       template<typename _InputIterator>
1981         void
1982         _M_range_insert_aux(iterator __pos, _InputIterator __first,
1983                             _InputIterator __last, std::input_iterator_tag);
1984
1985       // called by the second insert_dispatch above
1986       template<typename _ForwardIterator>
1987         void
1988         _M_range_insert_aux(iterator __pos, _ForwardIterator __first,
1989                             _ForwardIterator __last, std::forward_iterator_tag);
1990
1991       // Called by insert(p,n,x), and the range insert when it turns out to be
1992       // the same thing.  Can use fill functions in optimal situations,
1993       // otherwise passes off to insert_aux(p,n,x).
1994       void
1995       _M_fill_insert(iterator __pos, size_type __n, const value_type& __x);
1996
1997       // called by insert(p,x)
1998 #if __cplusplus < 201103L
1999       iterator
2000       _M_insert_aux(iterator __pos, const value_type& __x);
2001 #else
2002       template<typename... _Args>
2003         iterator
2004         _M_insert_aux(iterator __pos, _Args&&... __args);
2005 #endif
2006
2007       // called by insert(p,n,x) via fill_insert
2008       void
2009       _M_insert_aux(iterator __pos, size_type __n, const value_type& __x);
2010
2011       // called by range_insert_aux for forward iterators
2012       template<typename _ForwardIterator>
2013         void
2014         _M_insert_aux(iterator __pos,
2015                       _ForwardIterator __first, _ForwardIterator __last,
2016                       size_type __n);
2017
2018
2019       // Internal erase functions follow.
2020
2021       void
2022       _M_destroy_data_aux(iterator __first, iterator __last);
2023
2024       // Called by ~deque().
2025       // NB: Doesn't deallocate the nodes.
2026       template<typename _Alloc1>
2027         void
2028         _M_destroy_data(iterator __first, iterator __last, const _Alloc1&)
2029         { _M_destroy_data_aux(__first, __last); }
2030
2031       void
2032       _M_destroy_data(iterator __first, iterator __last,
2033                       const std::allocator<_Tp>&)
2034       {
2035         if (!__has_trivial_destructor(value_type))
2036           _M_destroy_data_aux(__first, __last);
2037       }
2038
2039       // Called by erase(q1, q2).
2040       void
2041       _M_erase_at_begin(iterator __pos)
2042       {
2043         _M_destroy_data(begin(), __pos, _M_get_Tp_allocator());
2044         _M_destroy_nodes(this->_M_impl._M_start._M_node, __pos._M_node);
2045         this->_M_impl._M_start = __pos;
2046       }
2047
2048       // Called by erase(q1, q2), resize(), clear(), _M_assign_aux,
2049       // _M_fill_assign, operator=.
2050       void
2051       _M_erase_at_end(iterator __pos)
2052       {
2053         _M_destroy_data(__pos, end(), _M_get_Tp_allocator());
2054         _M_destroy_nodes(__pos._M_node + 1,
2055                          this->_M_impl._M_finish._M_node + 1);
2056         this->_M_impl._M_finish = __pos;
2057       }
2058
2059       iterator
2060       _M_erase(iterator __pos);
2061
2062       iterator
2063       _M_erase(iterator __first, iterator __last);
2064
2065 #if __cplusplus >= 201103L
2066       // Called by resize(sz).
2067       void
2068       _M_default_append(size_type __n);
2069
2070       bool
2071       _M_shrink_to_fit();
2072 #endif
2073
2074       //@{
2075       /// Memory-handling helpers for the previous internal insert functions.
2076       iterator
2077       _M_reserve_elements_at_front(size_type __n)
2078       {
2079         const size_type __vacancies = this->_M_impl._M_start._M_cur
2080                                       - this->_M_impl._M_start._M_first;
2081         if (__n > __vacancies)
2082           _M_new_elements_at_front(__n - __vacancies);
2083         return this->_M_impl._M_start - difference_type(__n);
2084       }
2085
2086       iterator
2087       _M_reserve_elements_at_back(size_type __n)
2088       {
2089         const size_type __vacancies = (this->_M_impl._M_finish._M_last
2090                                        - this->_M_impl._M_finish._M_cur) - 1;
2091         if (__n > __vacancies)
2092           _M_new_elements_at_back(__n - __vacancies);
2093         return this->_M_impl._M_finish + difference_type(__n);
2094       }
2095
2096       void
2097       _M_new_elements_at_front(size_type __new_elements);
2098
2099       void
2100       _M_new_elements_at_back(size_type __new_elements);
2101       //@}
2102
2103
2104       //@{
2105       /**
2106        *  @brief Memory-handling helpers for the major %map.
2107        *
2108        *  Makes sure the _M_map has space for new nodes.  Does not
2109        *  actually add the nodes.  Can invalidate _M_map pointers.
2110        *  (And consequently, %deque iterators.)
2111        */
2112       void
2113       _M_reserve_map_at_back(size_type __nodes_to_add = 1)
2114       {
2115         if (__nodes_to_add + 1 > this->_M_impl._M_map_size
2116             - (this->_M_impl._M_finish._M_node - this->_M_impl._M_map))
2117           _M_reallocate_map(__nodes_to_add, false);
2118       }
2119
2120       void
2121       _M_reserve_map_at_front(size_type __nodes_to_add = 1)
2122       {
2123         if (__nodes_to_add > size_type(this->_M_impl._M_start._M_node
2124                                        - this->_M_impl._M_map))
2125           _M_reallocate_map(__nodes_to_add, true);
2126       }
2127
2128       void
2129       _M_reallocate_map(size_type __nodes_to_add, bool __add_at_front);
2130       //@}
2131
2132 #if __cplusplus >= 201103L
2133       // Constant-time, nothrow move assignment when source object's memory
2134       // can be moved because the allocators are equal.
2135       void
2136       _M_move_assign1(deque&& __x, /* always equal: */ true_type) noexcept
2137       {
2138         this->_M_impl._M_swap_data(__x._M_impl);
2139         __x.clear();
2140         std::__alloc_on_move(_M_get_Tp_allocator(), __x._M_get_Tp_allocator());
2141       }
2142
2143       void
2144       _M_move_assign1(deque&& __x, /* always equal: */ false_type)
2145       {
2146         constexpr bool __move_storage =
2147           _Alloc_traits::_S_propagate_on_move_assign();
2148         _M_move_assign2(std::move(__x),
2149                         integral_constant<bool, __move_storage>());
2150       }
2151
2152       // Destroy all elements and deallocate all memory, then replace
2153       // with elements created from __args.
2154       template<typename... _Args>
2155       void
2156       _M_replace_map(_Args&&... __args)
2157       {
2158         // Create new data first, so if allocation fails there are no effects.
2159         deque __newobj(std::forward<_Args>(__args)...);
2160         // Free existing storage using existing allocator.
2161         clear();
2162         _M_deallocate_node(*begin()._M_node); // one node left after clear()
2163         _M_deallocate_map(this->_M_impl._M_map, this->_M_impl._M_map_size);
2164         this->_M_impl._M_map = nullptr;
2165         this->_M_impl._M_map_size = 0;
2166         // Take ownership of replacement memory.
2167         this->_M_impl._M_swap_data(__newobj._M_impl);
2168       }
2169
2170       // Do move assignment when the allocator propagates.
2171       void
2172       _M_move_assign2(deque&& __x, /* propagate: */ true_type)
2173       {
2174         // Make a copy of the original allocator state.
2175         auto __alloc = __x._M_get_Tp_allocator();
2176         // The allocator propagates so storage can be moved from __x,
2177         // leaving __x in a valid empty state with a moved-from allocator.
2178         _M_replace_map(std::move(__x));
2179         // Move the corresponding allocator state too.
2180         _M_get_Tp_allocator() = std::move(__alloc);
2181       }
2182
2183       // Do move assignment when it may not be possible to move source
2184       // object's memory, resulting in a linear-time operation.
2185       void
2186       _M_move_assign2(deque&& __x, /* propagate: */ false_type)
2187       {
2188         if (__x._M_get_Tp_allocator() == this->_M_get_Tp_allocator())
2189           {
2190             // The allocators are equal so storage can be moved from __x,
2191             // leaving __x in a valid empty state with its current allocator.
2192             _M_replace_map(std::move(__x), __x.get_allocator());
2193           }
2194         else
2195           {
2196             // The rvalue's allocator cannot be moved and is not equal,
2197             // so we need to individually move each element.
2198             this->assign(std::__make_move_if_noexcept_iterator(__x.begin()),
2199                          std::__make_move_if_noexcept_iterator(__x.end()));
2200             __x.clear();
2201           }
2202       }
2203 #endif
2204     };
2205
2206
2207   /**
2208    *  @brief  Deque equality comparison.
2209    *  @param  __x  A %deque.
2210    *  @param  __y  A %deque of the same type as @a __x.
2211    *  @return  True iff the size and elements of the deques are equal.
2212    *
2213    *  This is an equivalence relation.  It is linear in the size of the
2214    *  deques.  Deques are considered equivalent if their sizes are equal,
2215    *  and if corresponding elements compare equal.
2216   */
2217   template<typename _Tp, typename _Alloc>
2218     inline bool
2219     operator==(const deque<_Tp, _Alloc>& __x,
2220                          const deque<_Tp, _Alloc>& __y)
2221     { return __x.size() == __y.size()
2222              && std::equal(__x.begin(), __x.end(), __y.begin()); }
2223
2224   /**
2225    *  @brief  Deque ordering relation.
2226    *  @param  __x  A %deque.
2227    *  @param  __y  A %deque of the same type as @a __x.
2228    *  @return  True iff @a x is lexicographically less than @a __y.
2229    *
2230    *  This is a total ordering relation.  It is linear in the size of the
2231    *  deques.  The elements must be comparable with @c <.
2232    *
2233    *  See std::lexicographical_compare() for how the determination is made.
2234   */
2235   template<typename _Tp, typename _Alloc>
2236     inline bool
2237     operator<(const deque<_Tp, _Alloc>& __x,
2238               const deque<_Tp, _Alloc>& __y)
2239     { return std::lexicographical_compare(__x.begin(), __x.end(),
2240                                           __y.begin(), __y.end()); }
2241
2242   /// Based on operator==
2243   template<typename _Tp, typename _Alloc>
2244     inline bool
2245     operator!=(const deque<_Tp, _Alloc>& __x,
2246                const deque<_Tp, _Alloc>& __y)
2247     { return !(__x == __y); }
2248
2249   /// Based on operator<
2250   template<typename _Tp, typename _Alloc>
2251     inline bool
2252     operator>(const deque<_Tp, _Alloc>& __x,
2253               const deque<_Tp, _Alloc>& __y)
2254     { return __y < __x; }
2255
2256   /// Based on operator<
2257   template<typename _Tp, typename _Alloc>
2258     inline bool
2259     operator<=(const deque<_Tp, _Alloc>& __x,
2260                const deque<_Tp, _Alloc>& __y)
2261     { return !(__y < __x); }
2262
2263   /// Based on operator<
2264   template<typename _Tp, typename _Alloc>
2265     inline bool
2266     operator>=(const deque<_Tp, _Alloc>& __x,
2267                const deque<_Tp, _Alloc>& __y)
2268     { return !(__x < __y); }
2269
2270   /// See std::deque::swap().
2271   template<typename _Tp, typename _Alloc>
2272     inline void
2273     swap(deque<_Tp,_Alloc>& __x, deque<_Tp,_Alloc>& __y)
2274     { __x.swap(__y); }
2275
2276 #undef _GLIBCXX_DEQUE_BUF_SIZE
2277
2278 _GLIBCXX_END_NAMESPACE_CONTAINER
2279 } // namespace std
2280
2281 #endif /* _STL_DEQUE_H */